package com.atguigu.gmall.index.controller;

import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.index.feign.GmallPmsClient;
import com.atguigu.gmall.index.service.IndexService;
import com.atguigu.gmall.pms.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/")
public class IndexController {
    @Autowired
    GmallPmsClient pmsClient;
    @Autowired
    IndexService indexService;
    //http://api.gmall.com/index/cates/5
    //查询一级分类的二级三级子分类
    @ResponseBody
    @GetMapping("/index/cates/{c1id}")
    public ResponseVo queryCatesAndsubs(@PathVariable("c1id") Long c1id) {
        /*
            二级分类：
                标题
                id
                subs：三级分类集合
         */
        List<CategoryEntity> cates = indexService.queryCatesAndsubs(c1id);
        return ResponseVo.ok(cates);
    }

    @GetMapping({"/" ,"/index.html" , "index" , "default" , "default.html"})
    public String index(Model model) {
        /*
        categories: 跳转到index.html页面之前需要查询一级分类数据存到request中
         */
        //需要远程访问 pms服务
        ResponseVo<List<CategoryEntity>> responseVo = pmsClient.queryCategoriesByParentId(0L);
        List<CategoryEntity> categoryEntities = responseVo.getData();
        model.addAttribute("categories", categoryEntities);
        return "index";
    }

    //测试redis模板类的使用
    @ResponseBody
    @GetMapping("testRedis")
    public ResponseVo testRedisTemplate(){
        indexService.testRedisTemplate();
        return ResponseVo.ok();
    }

    @ResponseBody
    @GetMapping("testLock")
    public ResponseVo testLock(){
        indexService.testLock();
        return ResponseVo.ok();
    }
    @ResponseBody
    @GetMapping("testLock2")
    public ResponseVo testLock2(){
        indexService.testLock2();
        return ResponseVo.ok();
    }

    @ResponseBody
    @GetMapping("testCdl1")
    public ResponseVo testCdl1(){
        indexService.testCdl1();
        return ResponseVo.ok();
    }
    @ResponseBody
    @GetMapping("testCdl2")
    public ResponseVo testCdl2(){
        indexService.testCdl2();
        return ResponseVo.ok();
    }

}
